###############################################################################

#  Different Time Frame between 1990 and 2021 (Table A9)

###############################################################################

sink(file = "log_file5.txt", type = "output")

require('tidyverse') # for simple data wrangling
require('rio')       # for simple data importing
require(magrittr)
require(dplyr)
require(stringr)
require(ggplot2)
require(tidyr)
require(MASS)
require(stargazer)
require(coefplot)
require(sjPlot)
require(margins)
require(plm)
require(estimatr)
require(lmtest)
require(lme4)
require(pscl)
require(jtools)
library(broom)      # For converting models into tables
library(rdrobust)   # For robust nonparametric regression discontinuity
library(rddensity) # For nonparametric regression discontinuity density tests
library(rddtools)
library(huxtable)   # For side-by-side regression tables
library(rdd)
require(haven)
require(plyr)
require(cowplot)
library(data.table)
require(gridExtra)
library(estimatr)
library(readr)
require(grid)
require(ggpubr)


# Period: 1990-2021 
# Replicating Table A9 

#download data
manifesto_pop<-read.csv('data2.csv')

#creating variable for country FE
manifesto_pop$state.f = factor(manifesto_pop$countryname)
manifesto_pop$state.d = model.matrix(~manifesto_pop$state.f+0)
manifesto_pop$military_l<-log(manifesto_pop$military+1)


#-----------------------------------------------------------------------------
# Position: Military Positive - Military Negative Per104-Per105 (Fuzzy)
#-----------------------------------------------------------------------------
manifesto_pop1<-manifesto_pop%>%dplyr::select(dif_l1, military_l,state.d, countryname,treatment)
manifesto_pop1<-manifesto_pop1[complete.cases(manifesto_pop1), ]
mil_f<- rdd_data(x=manifesto_pop1$dif_l1, y=manifesto_pop1$military_l, z=manifesto_pop1$treatment, covar=cbind(manifesto_pop1$state.d), cutpoint=0, data=manifesto_pop1) 
mil_f2<- rdd_data(x=dif_l1, y=military_l, z=manifesto_pop1$treatment, cutpoint=0, data=manifesto_pop1) 

#parametric with fixed effect
summary(f<-rdd_reg_lm(rdd_object=mil_f, covar.opt = list(strategy = c("include"), slope = c( "separate")), order=1))  #fuzzy w/FE
coeftest(f, vcov=vcovHC(f,type="HC0",cluster="countryname"))
#parametric without fixed effect
summary(f2<-rdd_reg_lm(rdd_object=mil_f2, order=1))  
coeftest(f2, vcov=vcovHC(f2,type="HC0",cluster="countryname"))

#nonparametric with fixed effect
summary(rdbwselect_2014(y = manifesto_pop1$military_l, x = manifesto_pop1$dif_l1, c=0, bwselect="CCT"))
summary(rdrobust(y = manifesto_pop1$military_l, x = manifesto_pop1$dif_l1, c=0, kernel = "tri",level = 95, p=2, all=TRUE, covs=cbind(manifesto_pop1$state.d, manifesto_pop1$Crimea),h=2.02, b=3.138,cluster=manifesto_pop1$countryname, fuzzy = manifesto_pop1$treatment))
#nonparametric without fixed effect
summary(rdrobust(y = manifesto_pop1$military_l, x = manifesto_pop1$dif_l1, c=0, kernel = "tri",level = 95, p=2, all=TRUE,h= 2.051, b=3.763, cluster=manifesto_pop1$countryname, fuzzy = manifesto_pop1$treatment))

sink()